package com.example.onlineeducation.ui.mine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.ExamConstants
import com.example.onlineeducation.data.model.ExamRecordListDataModel
import com.example.onlineeducation.data.model.ScoreRecordListDataModel
import com.example.onlineeducation.data.model.ScoreRecordListDataModel.Companion.diffCallback
import com.example.onlineeducation.databinding.ExamRecordActicityBinding
import com.example.onlineeducation.databinding.ExamRecordItemBinding
import com.example.onlineeducation.databinding.ScoreRecordActicityBinding
import com.example.onlineeducation.databinding.ScoreRecordItemBinding
import com.example.onlineeducation.mapper.ScoreRecordListMapper
import com.example.onlineeducation.viewmodel.ExamRecordVM
import com.yc.jetpacklib.extension.*
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil

/*
* 积分记录列表
* */
@SuppressLint("SetTextI18n")
class ScoreRecordActivity : BaseActivity<ScoreRecordActicityBinding>(ScoreRecordActicityBinding::inflate) {
    private val mViewModel: ExamRecordVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<ScoreRecordListDataModel>

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, ScoreRecordActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mAdapter: YcPagingDataAdapter<ScoreRecordListDataModel, ScoreRecordItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        ScoreRecordItemBinding::inflate, ScoreRecordListDataModel.diffCallback
    ) {
        mOnUpdate = { item ->
            tvRemarkName.text = item.remark
            tvScore.text = item.points
            tvTime.text = item.createTime
        }
    }

    override fun ScoreRecordActicityBinding.initView() {
        actionbarLayout.setTitle(titleName = "积分明细")
        rvList.apply {
            adapter = mAdapter
            ycInitLinearLayoutManage()
        }
        mRefreshUtil = YcRefreshSpecialPagingUtil<ScoreRecordListDataModel>(this@ScoreRecordActivity)
            .build(mAdapter, srlList, rvList, flRefresh) {
                mRefreshCall = {
                    getData()
                }
            }
        mRefreshUtil.startRefresh()

        mViewModel.mScoreRecordListDataModel.observe {
            mRefreshUtil.acSetPagingData(it)
        }
    }

    private fun getData() {
        mViewModel.getScoreRecordListPager()
    }
}